package com.mk.sso.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.entity.sso.UserEntity;
import com.zxw.common.entity.sso.UserListQuery;
import com.zxw.common.entity.sso.UserQuery;
import com.mk.sso.mapper.UserMapper;
import com.mk.sso.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-11-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {


	/**
	 * 添加
	 *
	 * @param userQuery UserQuery
	 * @return 是否成功 true|false
	 */
	@Override
	public boolean save(UserQuery userQuery) {
		UserEntity userEntity = new UserEntity();
		BeanUtils.copyProperties(userQuery, userEntity);
		return super.save(userEntity);
	}

	/**
	 * 批量添加
	 *
	 * @param list List<UserQuery>
	 * @return 是否成功 true|false
	 */
	@Override
	public boolean saveBatch(List<UserQuery> list) {
		List<UserEntity> collect = list.stream().map(item -> {
			UserEntity userEntity = new UserEntity();
			BeanUtils.copyProperties(item, userEntity);
			return userEntity;
		}).collect(Collectors.toList());
		return super.saveBatch(collect);
	}

	/**
	 * 根据ID集合批量删除
	 *
	 * @param ids id集合
	 * @return 是否成功 true|false
	 */
	@Override
	public boolean removeByIds(List<String> ids) {
		return super.removeByIds(ids);
	}

	/**
	 * 根据ID修改
	 *
	 * @param userQuery UserQuery
	 * @return 是否成功 true|false
	 */
	@Override
	public boolean updateById(UserQuery userQuery) {
		UserEntity userEntity = new UserEntity();
		BeanUtils.copyProperties(userQuery, userEntity);
		return super.updateById(userEntity);
	}

	/**
	 * 根据ID查询数据
	 *
	 * @param id ID
	 * @return UserDO
	 */
	@Override
	public UserEntity getById(String id) {
		return super.getById(id);
	}

	/**
	 * 条件分页查询
	 *
	 * @param userListQuery UserListQuery
	 * @return IPage<UserDO>
	 */
	@Override
	public IPage<UserEntity> list(UserListQuery userListQuery) {
		IPage<UserEntity> pageInfo = new Page<>(userListQuery.getCurrent(), userListQuery.getSize());
		QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(UserEntity::getStatus, 1)
				.orderByDesc(UserEntity::getCreateTime);
		return super.page(pageInfo, queryWrapper);
	}

}
